#include "MainForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Uix;
using namespace Osp::Locations;

MainForm::MainForm(void)
{
}

MainForm::~MainForm(void)
{
}

bool
MainForm::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_MAINFORM");



	return true;
}

result
MainForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	__pScrollPanel1 = static_cast<ScrollPanel*>(this->GetControl("SCROLLPANEL1"));
	__pScrollPanel2 = static_cast<ScrollPanel*>(this->GetControl("SCROLLPANEL2"));
	__pScrollPanel2->SetShowState(false);

	Tab* __pTab = GetTab();
	if (__pTab) {
		__pTab->AddItem(L"Mai", ID_EGY);
		__pTab->AddItem(L"Elorejelzes", ID_KETTO);
		__pTab->AddActionEventListener(*this);
	}

	__pButtonOk = static_cast<Button *>(GetControl(L"IDC_BUTTON_OK"));
	if (__pButtonOk != null)
	{
		__pButtonOk->SetActionId(ID_BUTTON_OK);
		__pButtonOk->AddActionEventListener(*this);
	}

	weathersensor.Construct(*this);
	__pFokLabel = static_cast<Label *>(__pScrollPanel1->GetControl(L"FOK"));
	__pVarosLabel = static_cast<Label *>(__pScrollPanel1->GetControl(L"VAROS"));
	__pRealLabel = static_cast<Label *>(__pScrollPanel1->GetControl(L"REALFEEL"));
	__pSzelLabel = static_cast<Label *>(__pScrollPanel1->GetControl(L"WIND"));
	__pCondLabel = static_cast<Label *>(__pScrollPanel1->GetControl(L"CONDITION"));

	__pMyList = static_cast<List *>(__pScrollPanel2->GetControl(L"MYLIST"));

	r=weathersensor.RequestCurrentWeather(L"Budapest");

	locProvider = new LocationProvider();
	locProvider->Construct(LOC_METHOD_HYBRID);
	locProvider->RequestLocationUpdates(*this, 5, false);

	longitude = latitude = 0;

	return r;
}

result
MainForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void
MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
	case ID_BUTTON_OK:
		{
			result r;
			String s = ("Seoul, South Korea");
			r=weathersensor.RequestCurrentWeather(L"Budapest");
			if (IsFailed(r)) AppLog("-.-");
			AppLog("OK Button is clicked! \n");
		}
		break;
	case ID_EGY:
		{
			result r;
			String s = ("Seoul, South Korea");

			r=weathersensor.RequestCurrentWeather(longitude,latitude);
			if (IsFailed(r)) AppLog("-.-");

			__pScrollPanel1->SetShowState(true);
			__pScrollPanel2->SetShowState(false);
		}
		break;
	case ID_KETTO:
		{
			result r;
			String s = ("Seoul, South Korea");
			r=weathersensor.RequestWeatherForecast(longitude,latitude);
			if (IsFailed(r)) AppLog("-.-");

			__pScrollPanel1->SetShowState(false);
			__pScrollPanel2->SetShowState(true);
		}
		break;
	default:
		break;
	}
}

void
MainForm::OnCurrentWeatherReceived(CurrentWeather& currentWeather, result r)
{
	AppLog("weatherdatareceived! \n");
	float fok,real,c,szel,irany;
	currentWeather.GetTemperature(fok,real,c);
	String varos;
	currentWeather.GetLocation(varos);
	currentWeather.GetWind(szel,irany);

	__pRealLabel->SetText(Osp::Base::Float::ToString(real));
	__pFokLabel->SetText(Osp::Base::Float::ToString(fok));
	__pVarosLabel->SetText(varos);
	__pCondLabel->SetText(Osp::Base::Float::ToString(irany));
	__pSzelLabel->SetText(Osp::Base::Float::ToString(szel));
	RequestRedraw(true);
}

void
MainForm::OnWeatherForecastReceived(Osp::Base::Collection::IList *pWeatherForecastList, result r)
{
	AppLog("weatherforecastreceived! \n");
	__pMyList->RemoveAllItems();
	int var;
	WeatherForecast * wf;
	Osp::Base::DateTime date;
	float high, low;
	String temp;
	String asd = " / ";
	for (var = 0; var < 7; var++) {
		wf = dynamic_cast<WeatherForecast *>(pWeatherForecastList->GetAt(var));
		wf->GetDate(date);
		temp = Osp::Base::Float::ToString(high);
		temp.Append(asd);
		temp.Append(Osp::Base::Float::ToString(low));
		wf->GetTemperature(high, low);
		__pMyList->AddItem(&date.ToString(),&temp,null,null,100);
	}
	RequestRedraw(true);
}

void
MainForm::OnLocationUpdated(Location& location)
{
    if(location.GetQualifiedCoordinates()!=null)
    {
    	AppLog("beleptem");
        const QualifiedCoordinates *q = location.GetQualifiedCoordinates();
        double a= q->GetLatitude();
        double b= q->GetLongitude();
        latitude = (float) a;
        longitude = (float) b;
        AppLog("%f %f", longitude, latitude);
        RequestRedraw(true);
    }
}

void
MainForm::OnProviderStateChanged(LocProviderState newState){}
